<template>
  <div class="my">
    <div class="user-bg">
      <div class="title">个人中心</div>
      <div class="user-box">
        <div class="user-message">
          <div class="user-img">
            <img v-if="this.isLogin == 1"
              src="https://i0.hdslb.com/bfs/manga-static/ece06684ac7dc594f11fb0db9a8a981f78fa509e.gif" alt />
            <button v-else class="login-btn" @click="toLogin">立即登录</button>
          </div>
          <div class="user-briefIntroduction">
            <div class="user-top">
              <h3 class="name" v-if="this.isLogin == 1">{{ userMessageData[0].username }}</h3>
              <h3 class="name" v-else>用户名</h3>
              <span class="grade">{{ this.isLogin == 1 ? "v80" : "未定级" }}</span>
              <span class="my-icon"><van-icon size="20px" name="medal-o" color="#ff3f3f" /></span>
            </div>
            <div class="user-bottom">
              <p v-if="this.isLogin == 1">{{ userMessageData[0].phone }}</p>
              <p v-else>请登录查看手机号</p>
            </div>
          </div>
          <div class="user-setup" @click="openSetup">设置&gt;</div>
        </div>
        <div class="user-fans" v-if="this.isLogin == 1">
          <ul>
            <li v-for="(item, index) in userfansData" :key="index">
              <h3>{{ item.count }}</h3>
              <p>{{ item.name }}</p>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="user-list">
      <ul>
        <li v-for="(item, index) in userData" :key="index">
          <img :src="item.icon" />
          <h2>{{ item.title }}</h2>
        </li>
      </ul>
    </div>
    <div class="user-configuration">
      <div class="exit-box" v-if="isLogining">
        <button class="exit" @click="exitLogin">退出登录</button>
      </div>
    </div>
    <div class="user-setup-box" :class="showSetup? 'active' : ''">
      <Setupcp></Setupcp>
    </div>
    <Recommendcp title="更多推荐" :iconUrl="iconUrl" :showBtn=false :widthSize=47 :iconSize=30
      @changeTheNovel="getMoregRecommendFiction" :recommendFictionData="recommendFictionData"></Recommendcp>
  </div>
</template>

<script>
import { getUserInfo, getHomeRecommendFiction } from "@/api/api";
import { userfansData } from "@/js/data.js"
import Recommendcp from "@/components/Recommendcp.vue"
import Setupcp from "@/components/Setupcp.vue"
export default {
  name: "My",
  data() {
    return {
      userData: [],
      userfansData,
      userMessageData: [],
      recommendFictionData: [],
      isLogin: "",
      isLogining: false,
      iconUrl: require("../assets/moreRecomend.png")
    };
  },
  components: {
    Recommendcp,
    Setupcp
  },
  created() {
    //获取用户中心配置
    getUserInfo()
      .then(res => {
        res.data.data.confs.map((item, index) => {
          if (index < 3) {
            this.userData.push({
              title: item.title,
              icon: item.icon
            });
          }
        });
      })
      .catch(err => {
        console.log(err);
      });
    //获取登录信息
    this.getLogin()
  },
  computed: {
    showSetup() {
      return this.$store.state.showSetup
    }
  },
  methods: {
    //进行登录
    toLogin() {
      let path = this.$route.fullPath;
      localStorage.setItem("path", JSON.stringify(path));
      this.$router.push("/login");
    },
    //退出登录
    exitLogin() {
      localStorage.setItem("isLogin", JSON.stringify(0));
      this.$store.commit("getIsLogin", 0)
      this.getLogin()
    },
    //是否登录
    getLogin() {
      //判断是否登录,已登录即可收藏成功,否者需要登录在收藏
      this.isLogin = localStorage.getItem("isLogin");
      if (this.isLogin == 1) {
        this.isLogining = true
        //获取本地存储账号密码昵称
        let userinfo = JSON.parse(localStorage.getItem("userinfo"));
        if (userinfo) {
          this.userMessageData = userinfo
        } else {
          this.userMessageData = []
        }
      } else {
        this.isLogining = false
      }
    },
    //获取更多推荐漫画
    async getMoregRecommendFiction() {
      let res = await getHomeRecommendFiction()
      this.recommendFictionData = res.data.data.list
    },
    //打开设置
    openSetup() {
      this.$store.state.showSetup = true
    }
  }
};
</script>

<style lang="scss" scoped>
.my {
  .user-bg {
    height: 125px;
    background: linear-gradient(to right bottom,
        rgb(76, 141, 158) 5%,
        rgb(44, 98, 144) 95%);
    border-bottom-left-radius: 40px;
    border-bottom-right-radius: 40px;
    position: relative;

    .title {
      text-align: center;
      font-size: 18px;
      color: #fff;
      font-weight: bold;
      padding-top: 5px;
    }

    .user-box {
      position: absolute;
      bottom: -60px;
      left: 50%;
      transform: translate(-50%, 0);
      width: 80%;
      padding: 10px;
      background: #fff;
      border: 1px solid #f1f1f1;
      border-radius: 10px;
      z-index: 999;

      .user-message {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;

        .user-img {
          width: 70px;
          height: 70px;
          border-radius: 50%;
          overflow: hidden;
          border: 1px solid #f1f1f1;
          margin-right: 10px;

          img {
            width: 100%;
          }

          .login-btn {
            width: 100%;
            line-height: 70px;
            border: none;
            font-weight: bold;
            font-size: 14px;
          }
        }

        .user-briefIntroduction {
          flex: 1;

          .user-top {
            display: flex;
            align-items: center;
            margin-top: 5px;

            .name {
              font-size: 16px;
              font-weight: bold;
              margin: 0;
            }

            .grade {
              margin: 0 10px;
              background: #ff3f3f;
              padding: 0 5px;
              border-radius: 10px;
              color: #fff;
            }
          }

          .user-bottom {
            font-size: 14px;
            color: #777;

            p {
              margin: 20px 0 5px 0;
            }
          }
        }

        .user-setup {
          width: 40px;
          height: 15px;
          line-height: 15px;
          text-align: center;
          margin-top: 5px;
          padding: 2px;
          border: 1px solid #777;
          border-radius: 10px;
          color: #777;
        }
      }

      .user-fans {
        ul {
          display: flex;
          justify-content: space-around;
          text-align: center;

          li {
            h3 {
              color: #666;
              margin-bottom: 5px;
            }

            p {
              margin: 0;
              font-size: 16px;
              font-weight: bold
            }
          }
        }
      }
    }
  }

  .user-list {
    margin-top: 80px;

    ul {
      border: 1px solid #f1f1f1;
      margin: 0 20px;
      border-radius: 10px;
      padding: 0 5px;
      display: flex;
      justify-content: space-around;

      li {
        img {
          width: 50px;
          height: 50px;
          margin-right: 10px;
        }

        h2 {
          font-size: 12px;
          margin-top: 5px;
        }
      }
    }
  }

  .user-configuration {
    margin-top: 10px;

    .exit-box {
      text-align: center;
      margin: 10px 0;

      .exit {
        border: none;
        outline: none;
        width: 100px;
        height: 40px;
        font-size: 16px;
        border-radius: 4px;
        color: #2c3e50;
      }
    }
  }

  .user-setup-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    background: #fff;
    transform: translateX(100%);
    transition: transform .3s;
    z-index: 999;

    &.active {
      transform: translateX(0);
    }
  }
}
</style>